﻿using Microsoft.Extensions.Logging;
using VIA.Integration;
using VIA.SiMoJi.Domain.Inspection;

namespace VIA.SiMoJi.Domain.Electronics
{
    /// <summary>
    ///     控制面板。
    /// </summary>
    public class ControlPanel : Part
    {
        public ControlPanel(EventButton runButton, EventButton stopButton, EventButton resetButton)
        {
            RunButton = runButton;
            StopButton = stopButton;
            ResetButton = resetButton;

            Add(RunButton, StopButton, ResetButton);

            RunButton.Pressed += RunButton_Pressed;
            StopButton.Pressed += StopButton_Pressed;
            ResetButton.Pressed += ResetButton_Pressed;
        }

        public EventButton ResetButton { get; }
        public EventButton RunButton { get; }
        public EventButton StopButton { get; }

        protected virtual void ResetButton_Pressed(object sender, EventArgs e)
        {
            try
            {
                Root.Resume();
            }
            catch (Exception ex)
            {
                Logger.LogError("响应部件{0}重置功能时发生错误", ex, Id);
            }
        }

        protected virtual void RunButton_Pressed(object sender, EventArgs e)
        {
            try
            {
                Root.Run(RunningMode.Online);
            }
            catch (Exception ex)
            {
                Logger.LogError("响应部件{0}启动功能时发生错误", ex, Id);
            }
        }

        protected virtual void StopButton_Pressed(object sender, EventArgs e)
        {
            try
            {
                Root.Stop();
            }
            catch (Exception ex)
            {
                Logger.LogError("响应部件{0}暂停功能时发生错误", ex, Id);
            }
        }
    }
}
